import pickle,socket
import sys
import commands
from crvclient import doSetUntilDone

inf=open(sys.argv[1],"r")

vmid=pickle.load(inf)
serverstr=pickle.load(inf)

#set private ip
doSetUntilDone("%s/setVMAttr?vmid=%s&name=%s&value=%s"%(serverstr,vmid,
                                                            "PrivateIPV4",socket.gethostbyname(socket.gethostname())))

#set private hostname
doSetUntilDone("%s/setVMAttr?vmid=%s&name=%s&value=%s"%(serverstr,vmid,
                                                            "PrivateHostname",socket.gethostname()))

#set public ip, need to get it from metadata
cmdstr="curl -s http://169.254.169.254/latest/meta-data/public-ipv4"
(status,output)=commands.getstatusoutput(cmdstr)
print "Running command=%s, status=%d, output=%s"%(cmdstr,status,output)

doSetUntilDone("%s/setVMAttr?vmid=%s&name=%s&value=%s"%(serverstr,vmid,"PublicIPV4",output))

#set public hostname, need to get it from metadata
cmdstr="curl -s http://169.254.169.254/latest/meta-data/public-hostname"
(status,output)=commands.getstatusoutput(cmdstr)
print "Running command=%s, status=%d, output=%s"%(cmdstr,status,output)

doSetUntilDone("%s/setVMAttr?vmid=%s&name=%s&value=%s"%(serverstr,vmid,"PublicHostname",output))

doSetUntilDone("%s/setVMAttr?vmid=%s&name=Started&value=True"%(serverstr,vmid))

